package com.hermes.action.json;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.hermes.biz.common.model.comment.UserCommentDisplayUnitDO;
import com.hermes.biz.common.model.result.UserCommentResultDO;
import com.hermes.biz.common.model.result.UserResultDO;
import com.hermes.biz.common.model.user.UserCommentDO;
import com.hermes.biz.common.util.string.StringUtil;
import com.hermes.biz.service.item.ItemService;
import com.hermes.biz.service.user.UserCommentService;
import com.hermes.biz.service.user.UserService;
import com.hermes.common.action.AsynBaseAction;
import com.hermes.common.util.LoginHelperUtil;
import com.hermes.common.util.ParameterParserUtil;

/**
 * 
 * Pubish-Comment-For-Home
 * 
 * 
 */
public class DOAysnComment  extends AsynBaseAction{

	private static final long serialVersionUID = 1L;

	private Logger log = Logger.getLogger(DOAysnComment.class);
	
	private UserCommentService userCommentService;
	
	private ItemService itemService;
	
	private UserService userService;
	
	public String result;

	public String isRedirect;
	
	public UserCommentDisplayUnitDO userCommentDO;


	@Override
	public String execute() {
		
		
		String comment = ParameterParserUtil.getParameter("comment");
		String itemId = ParameterParserUtil.getParameter("itemid");
		String msgType = ParameterParserUtil.getParameter("type");
		String originId= ParameterParserUtil.getParameter("fromId");
		
		long itemIdNum=0;
		long userId=7;
		long originIdNum=0;
		int msgTypeNum=0;
		
		if(StringUtil.isNotBlank(itemId)&&StringUtil.isNumOnly(itemId)){
			itemIdNum = Long.parseLong(itemId);
		}
		if(StringUtil.isNotBlank(originId)&&StringUtil.isNumOnly(originId)){
			originIdNum = Long.parseLong(originId);
		}
		if(StringUtil.isNotBlank(msgType)&&StringUtil.isNumOnly(msgType)){
			msgTypeNum = Integer.parseInt(msgType);
		}

		HttpServletRequest request = ServletActionContext.getRequest();
		LoginHelperUtil loginHelper = new LoginHelperUtil(request);
		if(loginHelper.hasLogined()){
			log.error("user-log-in-report-in-detail");
			String username = loginHelper.getCurrentUserName();
			UserResultDO userResultDO = userService.getUserInfoByUserName(username);
			if(userResultDO.isSuccess()&& userResultDO.getModule()!=null){
				userId = userResultDO.getModule().getUserId();
			}
		}
		else{
			result="false";
			isRedirect="true";
			return SUCCESS;
		}
		
		log.error("user-comment-content:"+comment);
		
		try{
			UserCommentDO	commentDO = new UserCommentDO();
			commentDO.setComment(comment);
			commentDO.setCreateUserId(userId);
			commentDO.setItemId(itemIdNum);
			if(msgTypeNum>0 && originIdNum>0){
				commentDO.setType(msgTypeNum);
				commentDO.setOriginalCommentId(originIdNum);
				UserCommentResultDO ucrDO = userCommentService.getUserCommentInfoByCommentId(originIdNum);
				if(ucrDO.isSuccess()){
					commentDO.setOriginalUserId(ucrDO.getModule().getCreateUserId());
				}
			}
			else{
				commentDO.setType(0);
				commentDO.setOriginalCommentId(0);
				commentDO.setOriginalUserId(userId);
			}
			this.userCommentService.saveUserCommentInfo(commentDO);
		}
		catch(Exception e){
			result="false";
			isRedirect="false";
			return SUCCESS;
		}
		
		result="true";
		isRedirect="false";
	
		return SUCCESS;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public ItemService getItemService() {
		return itemService;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public UserCommentDisplayUnitDO getUserCommentDO() {
		return userCommentDO;
	}

	public void setUserCommentDO(UserCommentDisplayUnitDO userCommentDO) {
		this.userCommentDO = userCommentDO;
	}
	
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getIsRedirect() {
		return isRedirect;
	}

	public void setIsRedirect(String isRedirect) {
		this.isRedirect = isRedirect;
	}

	public UserCommentService getUserCommentService() {
		return userCommentService;
	}

	public void setUserCommentService(UserCommentService userCommentService) {
		this.userCommentService = userCommentService;
	}
}
